/**
* <p>Title: Ellipses</p>
* <p>Description: CS 235 Assignment #2</p>
* @author Jacob Feyereisen
* @email jacob.feyereisen@my.uwrf.edu
* @date November 2nd 2011
* @team Derek Thompson, Jonathan Knapp, Tyler Enright
*/

package a2;

import java.util.Scanner;

/**
 * A simple textual interface example exercising the Ellipse class.
 */
public class MainEllipse {
  
  public static void main (String[] argv) {
    (new EllipseTUI()).start();
  }
}

/**
 * A simple text-based user interface for creating and exercising an Ellipse.
 */
class EllipseTUI {
 
  // User menu choices:
  static private enum Choice
  {
	VIEW_ELLIPSE,
	NO_CHOICE,
	MAJ_AXS,
	MIN_AXS,
	AREA,
	CIRCUMF,
	NEW,
	EXIT;
  }
  private Choice choice;
  private int numberOfEllipse;
  private EllipseArray ellipses;
  private Scanner in;
  private java.io.PrintStream out = System.out;

  /**
   * Create a user interface.
   */
  public EllipseTUI () {
    this.ellipses = null;
    this.in = new Scanner(System.in);
  }

  /**
   * Run the user interface.
   * @ensure numberOfEllipse > 0
   */
  public void start () {
	System.out.println("How many ellipse would you like to store? ");
	numberOfEllipse = in.nextInt();
	assert(numberOfEllipse > 0);
    createEllipse(numberOfEllipse);
    choice = Choice.NO_CHOICE;
    while (choice != Choice.EXIT) {
      displayMenu();
      readIntWithPrompt("Enter choice: ");
      executeChoice();
    }
  }

  /**
   * Create an Ellipse with dimensions provided by the user.
   * @ensure major > 0 && minor > 0
   */
  private void createEllipse (int num) {
    int major = -1;
    int minor = -1;
    while (major < 0)
      major = readIntWithPrompt("Ellipse maj axis (a non-negative integer): ");
    while (minor < 0)
      minor = readIntWithPrompt("Ellipse min axis (a non-negative integer): ");
    assert(major > 0 && minor > 0);
    this.ellipses = new EllipseArray( new Ellipse(major,minor), numberOfEllipse);
  }
 
  /**
   * Display top level menu.
   */
  private void displayMenu () {
    out.println("\nEnter the number denoting the action to perform:" +
    "\nView Ellipse................" + 0 +
    "\nDisplay major axis.........." + 1 +
    "\nDisplay minor axis.........." + 2 +
    "\nDisplay area................" + 3 +
    "\nDisplay circumference......." + 4 +
    "\nCreate new Ellipse.........." + 5 +
    "\nExit........................" + 6 + "\n" );
  }

  /**
   * Execute user's menu choice.
   */
  private void executeChoice () {
    out.println();

    switch(choice)
    {
    case VIEW_ELLIPSE:
    	out.println("Choose Ellipse:\n");
    	for(int x = 0; x < ellipses.getNum();x++)
    		out.println("Ellipse..." + x);
    	
    	System.out.println(ellipses.toString(in.nextInt()));
    	break;
    case MAJ_AXS:
      out.println("Major axis is    " + ellipses.get().majorAxis());
      break;
    case MIN_AXS:
      out.println("Minor axis is    " + ellipses.get().minor_axis());
      break;
    case AREA:
      out.println("Area is          " + ellipses.get().area());
      break;
    case CIRCUMF:
      out.println("Circumference is " + ellipses.get().circumference());
      break;
    case NEW:
      createEllipse(numberOfEllipse);
      break;
    case EXIT:
      out.println("Goodbye.");
    }
  }
  
  /**
   * Read an int from system in with the speciifed prompt.
   * @require input >= 0 && input <=6
   */
  private int readIntWithPrompt (String prompt) {
	
    out.print(prompt); out.flush();
    while (!in.hasNextInt()) {
      in.nextLine();
      out.print(prompt); out.flush();
    }
    int input = in.nextInt();
    assert(input >= 0 && input <=6);
    switch(input)
    {
    	case 0:
    		choice = Choice.VIEW_ELLIPSE;
    		break;
    	case 1:
    		choice = Choice.MAJ_AXS;
    		break;
    	case 2:
    		choice = Choice.MIN_AXS;
    		break;
    	case 3:
    		choice = Choice.AREA;
    		break;
    	case 4:
    		choice = Choice.CIRCUMF;
    		break;
    	case 5:
    		choice = Choice.NEW;
    		break;
    	case 6:
    		choice = Choice.EXIT;
    		break;

    }
    
    in.nextLine();
    return input;
  }  
}

